/**    
 * 文件名：FirstPayPaymentHandler.java</br>
 *    
 * 版本信息：v1.0</br>
 * 日期：2017年7月12日</br>
 * © 2005-2017 雷技信息科技（上海）有限公司版权所有</br>
 *
 */
package com.logic.landseaserver.models.payment.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.exception.ResultCodes.PaymentResultCode;
import com.logic.landseaserver.common.util.Arith;
import com.logic.landseaserver.common.util.LandSeaUtil;
import com.logic.landseaserver.domain.BillPayment;
import com.logic.landseaserver.models.payment.PaymentConstant;
import com.logic.landseaserver.models.payment.PaymentConstant.BillTypeCode;
import com.logic.landseaserver.models.payment.bean.PaymentHelperBean;
import com.logic.landseaserver.models.payment.bean.PaymentRequest;
import com.logic.landseaserver.models.payment.service.AbstractPaymentRequestHandler;
import com.logic.landseaserver.persistence.read.BillReadMapper;
import com.logic.landseaserver.persistence.write.BillPaymentWriteMapper;
import com.logic.landseaserver.ws.dto.BillDTO;

/**
 *
 * 项目名称：system-server</br>
 * 类名称：FirstPayPaymentHandler</br>
 * 类描述：首期支付</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年7月12日 下午4:47:49</br>
 * 
 * @version 1.0
 *
 */
@Service(BillTypeCode.FIRST_PAY + PaymentConstant.PAYMENT_REQUEST_SUFFIX)
public class FirstPayRequestHandler extends AbstractPaymentRequestHandler
{
    @Autowired
    private BillReadMapper billReadMapper;
    
    @Autowired
    private BillPaymentWriteMapper billPayWriteMapper;
    
    /**
     * [描述]：</br>
     *
     * @param payment
     * @param helperObject
     * @throws LandseaException
     *
     */
    @Override
    public void customCheckPayParam(PaymentRequest payment, PaymentHelperBean helperObject)
        throws LandseaException
    {
        // 检查输入金额是否合法
        Double payMoney = payment.getMoney();
        
        if (payMoney <= 0.00d)
        {
            // 支付金额小于等于0，非法
            String logInfo = getLog(payment, helperObject.getPaymentId());
            LOGGER.error(logInfo + " Pay money is invalied,payMoney=" + payMoney);
            throw LandseaException.createException(PaymentResultCode.E00140006);
        }
    }
    
    /**
     * [描述]：业务逻辑检查</br>
     *
     * @param payment
     * @param helperObject
     * @throws LandseaException
     *
     */
    @Override
    public void checkPayBillAndMoney(PaymentRequest payment, PaymentHelperBean helperObject)
        throws LandseaException
    {
        String logInfo = getLog(payment, helperObject.getPaymentId());
        Integer billId = payment.getBillId();
        
        try
        {
            // 此处账单必须存在
            BillDTO lsBillRecord = billReadMapper.selectByPrimaryKey(billId);
            if (null == lsBillRecord)
            {
                // 账单不存在
                LOGGER.error(logInfo + " Bill does not exists in ls_bill table,billId=" + billId);
                throw LandseaException.createException(PaymentResultCode.E00150002);
            }
            
            // 查询合同ID，后续会用到
            helperObject.setContractId(lsBillRecord.getContractId());
            
            // 应收款
            Double receivableMoney = lsBillRecord.getReceivableMoney();
            // 要转换成0.00保留两位小数
            receivableMoney = LandSeaUtil.get2Double(receivableMoney);
            // 已收款
            Double amountReceived = lsBillRecord.getAmountReceived();
            amountReceived = LandSeaUtil.get2Double(amountReceived);
            
            // 本次要支付的钱
            Double payMoney = payment.getMoney();
            payMoney = LandSeaUtil.get2Double(payMoney);
            
            // 要保证 总的支付不能多余总价格，你不能让租户吃亏
            // Double不加减，使用特殊的处理手段
            if (receivableMoney < Arith.add(amountReceived, payMoney))
            {
                // 总支付金额不能大于应收金额
                LOGGER.error(logInfo + " Total pay money can not more than receivable money. payMoney=" + payMoney
                    + ",received=" + amountReceived + ",receivableMoney=" + receivableMoney);
                throw LandseaException.createException(PaymentResultCode.E00150003);
            }
        }
        catch (LandseaException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            LOGGER.error(logInfo + " checkPayBillAndMoney occur a error.", e);
            throw LandseaException.createException(PaymentResultCode.E00150019);
        }
    }
    
    /**
     * [描述]：</br>
     *
     * @param payment
     * @param helperObject
     * @throws LandseaException
     *
     */
    @Override
    public void doLocalBusiness(BillPayment billPayment, PaymentHelperBean helperObject, Map<String,Object> resultMap)
        throws LandseaException
    {
        billPayWriteMapper.insertSelective(billPayment);
        
        // 将paymentId重新赋值
        helperObject.setPaymentId(billPayment.getId());
        resultMap.put("paymentId", billPayment.getId());
    }
    
    /**
     * [描述]：</br>
     *
     * @param payment
     * @param helperObject
     * @return
     *
     */
    @Override
    public Integer getPaymentId(BillPayment billPayment, PaymentHelperBean helperObject)
    {
        return helperObject.getPaymentId();
    }
    
    /**
     * [描述]：</br>
     *
     * @return
     *
     */
    @Override
    public String getProcessClassName()
    {
        return this.getClass().getSimpleName();
    }
}
